﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using Cosmetic.DataEntity;

namespace WebApp.Product
{
    public partial class iProduct : PageBase
    {
        IntegralManager m_IntegralManager = new IntegralManager();
        PurchaseManager thePurchaseManager = new PurchaseManager();
        ProductManager theProductManager = new ProductManager();
        UserManager theUserManager = new UserManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser == null)
            {
                Page.RegisterClientScriptBlock("key", "<script>window.top.location = '/Login.htm';</script>");
                return;
            }
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            this.lvProductSearch.DataSource = m_IntegralManager.GetiProductList();
            this.lvProductSearch.DataBind();
        }

        public string spliteStr(string title)
        {
            if (title.Length > 10)
            {
                title = title.Substring(0, 10) + "…";
            }
            return title;
        }
        public string spliteStrs(string title)
        {
            if (title.Length > 15)
            {
                title = title.Substring(0, 15) + "…";
            }
            return title;
        }

        protected void lvProductSearch_PagePropertiesChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void lvProductSearch_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            HiddenField hfePrice = e.Item.FindControl("hfEPrice") as HiddenField;
            if (e.CommandName == "AddCart")
            {//使用积分购买
                if (this.SignInUser.Integral < decimal.Parse(hfePrice.Value))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('积分不够!');", true);
                    return;
                }


                Order theOrder = new Order();
                theOrder.OrderId = Guid.NewGuid();
                theOrder.PayType = "积分";
                theOrder.UseIntegral = decimal.Parse(hfePrice.Value);
                theOrder.UserId = this.SignInUser.UserId;
                theOrder.CreateTime = DateTime.Now;
                theOrder.OrderNum = thePurchaseManager.CreateOrderNum();
                theOrder.Valid = true;
                theOrder.DeliveryType = "快递";
                theOrder.AddressId = new Guid("f0241e6d-b0af-4911-bc45-af3688dad592");
                theOrder.State = "已付款";

                thePurchaseManager.InsertOrderIntegral(theOrder);

                OrderDetail od = new OrderDetail();
                od.Count = 1;
                od.OrderDetailId = Guid.NewGuid();
                od.OrderId = theOrder.OrderId;
                od.ProductId = new Guid(e.CommandArgument.ToString());
                od.ProductCode = theProductManager.GetProductById(new Guid(e.CommandArgument.ToString())).ProductCode;

                thePurchaseManager.insertordersdelt(od);
                //this.SignInUser.Integral -= decimal.Parse(hfePrice.Value);

                Cosmetic.DataEntity.User user = theUserManager.GetUserById(this.SignInUser.UserId);
                user.Integral -= decimal.Parse(hfePrice.Value);
                theUserManager.Update();

                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('购买成功!');", true);
                return;
            }
        }
    }


}